<?php
namespace app\user\model;
use think\Request;
use app\common\model\Common;
use think\Db;
class User extends Common
{
	protected $autoWriteTimestamp  = true;
	protected $createTime = 'addtime';
	protected $updateTime = false;
	protected $append = ['visit_t_text'];

	/**
	 * 获取器
	 */
	public function getUTypeAttr($value)
	{
		switch($value){
			case 0: return '新客';break;
			case 1: return '老客';break;
			default: return '异常';break;
		}
	}
	public function getSexAttr($value)
	{
		switch($value){
			case 0: return '保密';break;
			case 1: return '男';break;
			case 2: return '女';break;
			default: return '异常';break;
		}
	}
	public function getAddtimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}
	public function getBuyTimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}
	public function getUserImgAttr($value)
	{
		return $value == '' ? config('parameter.user_img') : $value;
	}
	public function getVisitTTextAttr($value, $data)
	{
		if(isset($data['visit_t'])){
			return $data['visit_t'] > 0 ? date('Y-m-d', $data['visit_t']) : '---';
		}else{
			return $this->getVisitTime($data['visit_time'], $data['visit']);
		}
	}
	/**
	 * 添加用户
	 * @param array $user 添加的用户信息
	 */
	public function addUser($user)
	{	
		$user['user_sn'] = $this->setUserSn();
		$user['number'] = 1;
		return $this->validate('User.add','validate',false,'user')->allowField(true)->save($user);
	}
	/**
	 * 生成客户编号
	 */
	public function setUserSn()
	{
		$user_sn = $this->user_sn();
		if($this->where('user_sn',$user_sn)->count() > 0){
			return $this->setUserSn();
		}
		return $user_sn;
	}
	/**
	* 客户排行榜
	*/
	public function rankList($userWhere, $orderWhere, $page, $row)
	{
		if($orderWhere === false){
			return $this->alias('u')->field('u.*,0 as order_money')
			->where($userWhere)->order('u.user_id desc')->group('u.user_sn')->page($page, $row)->select();
		}else{
			$sql = $this->orderMoney($orderWhere);
			return $this->alias('u')->join($sql,'u.user_sn = o.user_sn','LEFT')
			->field('u.*,ifnull(o.order_money,0) as order_money')
			->where($userWhere)->order('o.order_money desc,u.user_id desc')->page($page, $row)->select();
		}
	}
	/**
	* 客户拜访名单列表
	*/
	public function userVisit($where, $order, $page, $row, $search)
	{
		$visitTimeSql = $this->visitTimeSql();
		return $this->alias('u')->join($visitTimeSql,'ti.user_id = u.user_id','LEFT')
		->where($where)->where($search)->page($page, $row)->order($order)->select();
	}
	/**
	* 客户拜访名单总数
	*/
	public function userVisitCount($where, $order, $search)
	{
		$visitTimeSql = $this->visitTimeSql();
		return $this->alias('u')->join($visitTimeSql,'ti.user_id = u.user_id','LEFT')
		->where($where)->where($search)->order($order)->count();
	}
	/********************************************* 私有 ************************************************/
	/**
	 * 客户编号
	 */
	private function user_sn()
	{
		return 'US'.rand(10000,99999);
	}
	/*
	* 客户购买金额
	*/
	private function orderMoney($where)
	{
		$sql = Db::name('order')->field('user_sn,ifnull(sum(order_money),0) as order_money')->where($where)->group('user_sn')->select(false);
		return "($sql) as o";
	}
	/**
	* 拜访时间
	* @param intger $start 开始拜访时间
	* @param intger $visit 拜访周期
	*/
	private function getVisitTime($start, $visit)
	{
		$now = strtotime(date('Y-m-d',time()));//当天时间戳
		$visit_time = intval(strval($visit*3600*24));//周期时间戳
		$time_diff = intval(strval($now)) - intval(strval($start)) + 3600*24;//时间差 多计算一天的开始时间
		if($time_diff >= $visit_time){/*@时间差大于等于周期时间*/
			$remain = intval(strval($time_diff)) % intval(strval($visit));//取余数
			$need_time = $visit_time - $remain;//需要添加的时间
		}else{/*@时间差小于周期时间*/
			$need_time = $visit_time - $time_diff;//需要添加的时间
		}
		return intval(strval($now)) +  $need_time;
	}
	/**
	* 拜访时间sql处理
	*/
	private function visitTimeSql()
	{
		// $field = "unix_timestamp(date_format(now(), '%Y-%m-%d')) as now_time,(now_time - visit_time + 3600*24) as time_diff,(visit*3600*24) as visitt,(case when time_diff >= visitt then (visitt - (time_diff mod visit)) else (visitt - time_diff) end) as need_time,(need_time + now_time)";

		/*@时间差*/
		$diff_sql = $this->field("(unix_timestamp(date_format(now(), '%Y-%m-%d')) - visit_time + 3600*24) as time_diff,user_id")->where('visit_time','<=',strtotime(date('Y-m-d',time())))->select(false);
		$diff_table = "($diff_sql) as diff";
		/*@需要天数*/
		$need_sql = $this->alias('u')->field("(case when diff.time_diff >= visit*3600*24 then (visit*3600*24 - (diff.time_diff mod visit)) else (visit*3600*24 - diff.time_diff) end) as need_time,u.user_id")->join($diff_table,'diff.user_id = u.user_id','LEFT')->select(false);
		$need_table = "($need_sql) as need";
		/*@拜访时间*/
		$sql = $this->alias('u')->field("(need.need_time + unix_timestamp(date_format(now(), '%Y-%m-%d'))) as visit_t,u.user_id")->join($need_table,'need.user_id = u.user_id','LEFT')->select(false);
		return "($sql) as ti";
	}

}